<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
import { queryCenter3 } from '@/api/business/largeScreen'

const animationDuration = 6000

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '270px'
    }
  },
  data() {
    return {
      chart: null,
      data: [2220, 1682, 2791, 3000, 4090, 3230, 2910],
      xdata: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
    }
  },
  mounted() {
    this.initChart('03')
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(zhlb) {
      this.chart = echarts.init(this.$el, 'macarons')

      queryCenter3({ zhlb: zhlb }).then(res => {
        console.log('queryCenter3:{}', res)
        this.chart.setOption({
          title: [
            { text: '每月已服务人次', textStyle: { color: '#fff', fontSize: '14' }}
          ],
          grid: {
            top: '17%',
            left: '2%',
            right: '2%',
            bottom: '3%',
            containLabel: true
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: { // 坐标轴指示器，坐标轴触发有效
              type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
            }
          },
          xAxis: {
            type: 'category',
            // boundaryGap: false,
            data: res.monthArray,
            triggerEvent: true,
            splitLine: {
              show: false
            },
            axisLine: {
              show: true,
              lineStyle: {
                width: 2,
                color: 'rgba(255,255,255,.6)'
              }
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              color: '#fff',
              fontSize: 12,
              fontWeight: 'bold',
              textShadowColor: '#000',
              textShadowOffsetY: 2
            }
          },
          yAxis: {
            nameTextStyle: {
              color: '#fff',
              fontSize: 14,
              textShadowColor: '#000',
              textShadowOffsetY: 2
            },
            type: 'value',
            splitLine: {
              show: true,
              lineStyle: {
                color: 'rgba(255,255,255,.2)'
              }
            },
            axisLine: {
              show: true,
              lineStyle: {
                width: 2,
                color: 'rgba(255,255,255,.6)'
              }
            },
            axisTick: {
              show: true
            },
            axisLabel: {
              color: '#fff',
              fontSize: 12,
              textShadowColor: '#000',
              textShadowOffsetY: 2
            }
          },
          series: [{
            name: '服务人次',
            data: res.dataArray,
            type: 'line',
            symbol: 'circle',
            symbolSize: 12,
            color: '#FEC201',
            lineStyle: {
              color: '#03E0F2'
            },
            label: {
              show: true,
              position: 'top',
              textStyle: {
                color: '#FEC201',
                fontSize: 18,
                fontWeight: 'bold'
              }
            },
            areaStyle: {
              color: 'rgba(1,98,133,0.6)'
            }
          }, {
            type: 'bar',
            animation: false,
            barWidth: 3,
            hoverAnimation: false,
            data: res.dataArray,
            tooltip: {
              show: false
            },
            itemStyle: {
              normal: {
                color: {
                  type: 'linear',
                  x: 0,
                  y: 0,
                  x2: 0,
                  y2: 1,
                  colorStops: [{
                    offset: 0,
                    color: '#91EAF2' // 0% 处的颜色
                  }, {
                    offset: 1,
                    color: '#074863' // 100% 处的颜色
                  }],
                  globalCoord: false // 缺省为 false
                },
                label: {
                  show: false
                }
              }
            }
          }]
        })
      })
    }
  }
}
</script>
